app.controller('sys_userEditController', ['$scope', 'httpWrapService','validationService', '$state', '$stateParams','dialogs',
                        'AlertFactory',
                    function ($scope, httpWrapService, validationService,  $state, $stateParams, dialogs, AlertFactory) {
    $scope.entity = {};

	$scope.selectRoles = {			
	};
	$scope.originalRoles = [];
    
    $scope.id = $stateParams.id;
    $scope.type = $stateParams.type || 'add';
    $scope.Alert = AlertFactory.newInstance();
    
    $scope.initRoles = function(){
    	if($scope.rolesTmp && $scope.originalRoles) {
    		var datas = $scope.rolesTmp;
    		var roleMaps = {};
    		for(var index in $scope.originalRoles) {
    			var roleId = $scope.originalRoles[index].id;
    			roleMaps[roleId] = roleId;
    		}
    		
    		for(var index in datas) {
    			var data = datas[index];
    			if(roleMaps[data.id]) {
    				data.checked = true;
    				$scope.selectRoles[data.id] = data.id;
    			}
    		}
    		
    		$scope.roles = datas;
    	}
    }

    if($scope.id) {
    	httpWrapService.post('system/user/view/' + $scope.id + '.do').then(function (res) {
    		$scope.entity = res.data.entity;
    		$scope.type = 'update';
    		$scope.confirmPassword = $scope.entity.password;
    		
    		$scope.originalRoles = res.data.map.roles;
    		$scope.initRoles();
    	});
    }
    
	httpWrapService.post('system/user/roles.do',{
    }).then(function(res2){
    	var datas = res2.data.data;
		$scope.rolesTmp = datas;
		$scope.initRoles();
    });


	$scope.setting = {
			check : {
				autoCheckTrigger : true,
				enable: true,
		        chkStyle: "checkbox",
		        chkboxType: {"Y": "p", "N": "s"}
			},
			callback : {
				onClick: function (event, treeId, treeNode, clickFlag) {
	            },
	            onCheck : function (event, treeId, treeNode){
	            	if(treeNode.checked === true) {
	            		$scope.selectRoles[treeNode.id] = treeNode.id;
	            	} else if(treeNode.checked === false) {
	            		delete $scope.selectRoles[treeNode.id];
	            	}
	            }
			}
		}

    $scope.unique = function(value){
        return validationService.unique({
            entityName : "common.rbac.po.User",
            field : "name",
            value : value,
            id : $scope.id
        })
    }

    $scope.save = function () {
        var roles = [];
        if($scope.selectRoles) {
            for(var index in $scope.selectRoles) {
                var obj = {
                    roleId : $scope.selectRoles[index],
                    userId : $scope.id
                }
                roles.push(obj);
            }
        }

        httpWrapService.defaultSave( 'system/user/' + $scope.type + '.do',{
            entity : $scope.entity,
            roles : roles
        }, $scope).then(function (res) {
            $state.go('app.system.user.list');
        });

        // 保存成功返回列表页面

    }

}]);
